
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Erlang 项式存储(ETS) · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="mnesia.html" />
    
    
    <link rel="prev" href="eex.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="plug.html">
            
                <a href="plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="eex.html">
            
                <a href="eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="4.3" data-path="ets.html">
            
                <a href="ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="mnesia.html">
            
                <a href="mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="debugging.html">
            
                <a href="debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Erlang 项式存储(ETS)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>Erlang &#x9879;&#x5F0F;&#x5B58;&#x50A8; (Erlang Term Storage&#xFF0C;&#x901A;&#x5E38;&#x7B80;&#x79F0; ETS) &#x662F; OTP &#x4E2D;&#x5185;&#x7F6E;&#x7684;&#x4E00;&#x4E2A;&#x529F;&#x80FD;&#x5F3A;&#x5927;&#x7684;&#x5B58;&#x50A8;&#x5F15;&#x64CE;&#xFF0C;&#x6211;&#x4EEC;&#x5728; Elixir &#x4E2D;&#x4E5F;&#x53EF;&#x4EE5;&#x5F88;&#x65B9;&#x4FBF;&#x5730;&#x4F7F;&#x7528;&#x3002;&#x672C;&#x6587;&#x5C06;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x4F7F;&#x7528; ETS &#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#x3002;</p>
<h2 id="&#x6982;&#x89C8;">&#x6982;&#x89C8;</h2>
<p>ETS &#x662F;&#x4E00;&#x4E2A;&#x9488;&#x5BF9; Elixir &#x548C; Erlang &#x5BF9;&#x8C61;&#x7684;&#x5065;&#x58EE;&#x7684;&#x5185;&#x5B58; (in-memory) &#x5B58;&#x50A8;&#xFF0C;&#x5E76;&#x4E14;&#x5185;&#x7F6E;&#x4E8E; OTP &#x4E2D;&#x3002;ETS &#x53EF;&#x4EE5;&#x5B58;&#x50A8;&#x5927;&#x91CF;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x540C;&#x65F6;&#x7EF4;&#x6301;&#x5E38;&#x6570;&#x65F6;&#x95F4;&#x7684;&#x6570;&#x636E;&#x8BBF;&#x95EE;&#x3002;</p>
<p>ETS &#x4E2D;&#x7684;&#x300C;&#x8868;&#x300D; (table) &#x662F;&#x7531;&#x5355;&#x72EC;&#x7684;&#x8FDB;&#x7A0B;&#x521B;&#x5EFA;&#x5E76;&#x62E5;&#x6709;&#x7684;&#x3002;&#x5F53;&#x8FD9;&#x4E2A;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#x65F6;&#xFF0C;&#x8FD9;&#x5F20;&#x8868;&#x4E5F;&#x5C31;&#x9500;&#x6BC1;&#x4E86;&#x3002;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B; ETS &#x9650;&#x5236;&#x6BCF;&#x4E2A;&#x8282;&#x70B9;&#x6700;&#x591A;&#x6709; 1400 &#x5F20;&#x8868;&#x3002;</p>
<h2 id="&#x5EFA;&#x8868;">&#x5EFA;&#x8868;</h2>
<p>&#x65B0;&#x7684;&#x8868;&#x7531; <code>new/2</code> &#x521B;&#x5EFA;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x8868;&#x540D;&#x4EE5;&#x53CA;&#x4E00;&#x7EC4;&#x9009;&#x9879;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8868;&#x6807;&#x8BC6;&#x7B26; (table identifier)&#xFF0C;&#x7528;&#x4E4B;&#x4E8E;&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x64CD;&#x4F5C;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x901A;&#x8FC7;&#x6635;&#x79F0;&#x6765;&#x5B58;&#x53D6;&#x7528;&#x6237;&#x7684;&#x8868;&#x6765;&#x505A;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; table = <span class="hljs-symbol">:ets</span>.new(<span class="hljs-symbol">:user_lookup</span>, [<span class="hljs-symbol">:set</span>, <span class="hljs-symbol">:protected</span>])
<span class="hljs-number">8212</span>
</code></pre>
<p>&#x7C7B;&#x4F3C; <code>GenServer</code>&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x540D;&#x5B57;&#x800C;&#x4E0D;&#x662F;&#x6807;&#x8BC6;&#x7B26;&#x6765;&#x8BBF;&#x95EE; ETS &#x8868;&#x3002;&#x8FD9;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x6DFB;&#x52A0; <code>:named_table</code> &#x9009;&#x9879;&#x3002;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x7528;&#x540D;&#x5B57;&#x6765;&#x8BBF;&#x95EE;&#x8FD9;&#x5F20;&#x8868;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.new(<span class="hljs-symbol">:user_lookup</span>, [<span class="hljs-symbol">:set</span>, <span class="hljs-symbol">:protected</span>, <span class="hljs-symbol">:named_table</span>])
<span class="hljs-symbol">:user_lookup</span>
</code></pre>
<h3 id="&#x8868;&#x7684;&#x7C7B;&#x578B;">&#x8868;&#x7684;&#x7C7B;&#x578B;</h3>
<p>ETS &#x63D0;&#x4F9B;&#x4E86;&#x56DB;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x8868;&#xFF1A;</p>
<ul>
<li><code>set</code> - &#x9ED8;&#x8BA4;&#x7684;&#x8868;&#x7C7B;&#x578B;&#x3002;&#x6BCF;&#x4E2A;&#x952E;(key)&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;&#x503C;(value)&#x3002;&#x952E;&#x662F;&#x552F;&#x4E00;&#x7684;&#x3002;</li>
<li><code>ordered_set</code> - &#x4E0E; <code>set</code> &#x7C7B;&#x4F3C;&#xFF0C;&#x4F46;&#x662F;&#x6309;&#x7167; Erlang/Elixir &#x9879;&#x5F0F;&#x6765;&#x6392;&#x5E8F;&#x3002;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#x8FD9;&#x91CC;&#x952E;&#x7684;&#x6BD4;&#x8F83;&#x65B9;&#x5F0F;&#x3002;&#x952E;&#x53EF;&#x4EE5;&#x4E0D;&#x540C;&#xFF0C;&#x53EA;&#x8981;&#xFF62;&#x76F8;&#x7B49;&#xFF63;&#x5373;&#x53EF;&#xFF0C;&#x4F8B;&#x5982; 1 &#x548C; 1.0 &#x5C31;&#x662F;&#xFF62;&#x76F8;&#x7B49;&#xFF63;&#x7684;&#x3002;</li>
<li><code>bag</code> - &#x6BCF;&#x4E2A;&#x952E;&#x53EF;&#x4EE5;&#x5305;&#x62EC;&#x591A;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x4F46;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x5728;&#x4E00;&#x4E2A;&#x952E;&#x4E2D;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x3002;</li>
<li><code>duplicate_bag</code> - &#x6BCF;&#x4E2A;&#x952E;&#x53EF;&#x4EE5;&#x5305;&#x62EC;&#x591A;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x4E5F;&#x5141;&#x8BB8;&#x5BF9;&#x8C61;&#x91CD;&#x590D;&#x3002;</li>
</ul>
<h3 id="&#x8BBF;&#x95EE;&#x63A7;&#x5236;">&#x8BBF;&#x95EE;&#x63A7;&#x5236;</h3>
<p>ETS &#x63D0;&#x4F9B;&#x7684;&#x8BBF;&#x95EE;&#x63A7;&#x5236;&#x673A;&#x5236;&#x8DDF;&#x6A21;&#x5757;&#x5DEE;&#x4E0D;&#x591A;&#xFF1A;</p>
<ul>
<li><code>public</code> - &#x6240;&#x6709;&#x8FDB;&#x7A0B;&#x90FD;&#x53EF;&#x4EE5;&#x8BFB;&#xFF0F;&#x5199;&#x3002;</li>
<li><code>protected</code> - &#x6240;&#x6709;&#x8FDB;&#x7A0B;&#x90FD;&#x53EF;&#x8BFB;&#x3002;&#x53EA;&#x6709;&#x62E5;&#x6709;&#x8005;&#x53EF;&#x4EE5;&#x5199;&#x3002;&#x8FD9;&#x662F;&#x9ED8;&#x8BA4;&#x7684;&#x914D;&#x7F6E;&#x3002;</li>
<li><code>private</code> - &#x53EA;&#x6709;&#x62E5;&#x6709;&#x8005;&#x53EF;&#x4EE5;&#x8BFB;&#xFF0F;&#x5199;&#x3002;</li>
</ul>
<h2 id="&#x8D44;&#x6E90;&#x7ADE;&#x4E89;&#xFF08;race-conditions&#xFF09;">&#x8D44;&#x6E90;&#x7ADE;&#x4E89;&#xFF08;Race Conditions&#xFF09;</h2>
<p>&#x5982;&#x679C;&#x591A;&#x4E8E;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x5199;&#x5165;&#x6570;&#x636E;&#x5230;&#x4E00;&#x4E2A;&#x8868; - &#x4E0D;&#x7BA1;&#x662F;&#x901A;&#x8FC7; <code>:public</code> &#x8BBF;&#x95EE;&#xFF0C;&#x6216;&#x8005;&#x901A;&#x8FC7;&#x62E5;&#x6709;&#x8005;&#x8FDB;&#x7A0B;&#x63A5;&#x6536;&#x6D88;&#x606F; - &#x8D44;&#x6E90;&#x7ADE;&#x4E89;&#x90FD;&#x662F;&#x53EF;&#x80FD;&#x53D1;&#x751F;&#x7684;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x4E24;&#x4E2A;&#x8FDB;&#x7A0B;&#x6BCF;&#x4E2A;&#x90FD;&#x5C1D;&#x8BD5;&#x8BFB;&#x53D6;&#x4E00;&#x4E2A;&#x503C;&#x4E3A; <code>0</code> &#x7684;&#x8BA1;&#x6570;&#x5668;&#xFF0C;&#x81EA;&#x589E;&#xFF0C;&#x7136;&#x540E;&#x5199;&#x5165; <code>1</code>&#xFF1B;&#x6700;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x5C31;&#x53EA;&#x53CD;&#x6620;&#x4E86;&#x4E00;&#x6B21;&#x81EA;&#x589E;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x8BA1;&#x6570;&#x5668;&#x6765;&#x8BF4;&#xFF0C;<a href="http://erlang.org/doc/man/ets.html#update_counter-3" target="_blank">:ets.update_counter/3</a> &#x63D0;&#x4F9B;&#x4E86;&#x539F;&#x5B50;&#x6027;&#x7684;&#x8BFB;&#x548C;&#x5199;&#x64CD;&#x4F5C;&#x3002;&#x5BF9;&#x4E8E;&#x5176;&#x5B83;&#x573A;&#x666F;&#xFF0C;&#x62E5;&#x6709;&#x8005;&#x8FDB;&#x7A0B;&#x53EF;&#x80FD;&#x8FD8;&#x662F;&#x5FC5;&#x987B;&#x6839;&#x636E;&#x6536;&#x5230;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#x539F;&#x5B50;&#x6027;&#x7684;&#x64CD;&#x4F5C;&#xFF0C;&#x6BD4;&#x5982; &#x201C;&#x628A;&#x5F53;&#x524D;&#x503C;&#xFF0C;&#x6DFB;&#x52A0;&#x5230;&#x5217;&#x8868;&#x91CC;&#x9762;&#x952E;&#x4E3A; <code>:results</code> &#x7684;&#x4F4D;&#x7F6E;&#x201D;&#x3002;</p>
<h2 id="&#x63D2;&#x5165;&#x6570;&#x636E;">&#x63D2;&#x5165;&#x6570;&#x636E;</h2>
<p>ETS &#x6CA1;&#x6709;&#x6A21;&#x5F0F; (Schema) &#x7684;&#x6982;&#x5FF5;&#x3002;&#x552F;&#x4E00;&#x7684;&#x9650;&#x5236;&#x662F;&#x6570;&#x636E;&#x9700;&#x8981;&#x4EE5;&#x5143;&#x7EC4;&#x7684;&#x5F62;&#x5F0F;&#x5B58;&#x653E;&#xFF0C;&#x5E76;&#x4E14;&#x5C06;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x4F5C;&#x4E3A;&#x952E;&#x3002;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>insert/2</code> &#x6765;&#x6DFB;&#x52A0;&#x65B0;&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.insert(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]})
<span class="hljs-keyword">true</span>
</code></pre>
<p>&#x5728; <code>set</code> &#x6216; <code>ordered_set</code> &#x4E0A;&#x76F4;&#x63A5;&#x6267;&#x884C; <code>insert/2</code> &#x4F1A;&#x8986;&#x76D6;&#x6389;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;&#x6570;&#x636E;&#x3002;&#x4F7F;&#x7528; <code>insert_new/2</code> &#x53EF;&#x4EE5;&#x907F;&#x514D;&#x6570;&#x636E;&#x8986;&#x76D6;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x8BE5;&#x51FD;&#x6570;&#x4F1A;&#x5728;&#x952E;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x65F6;&#x8FD4;&#x56DE; <code>false</code>&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.insert_new(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]})
<span class="hljs-keyword">false</span>
iex&gt; <span class="hljs-symbol">:ets</span>.insert_new(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-string">&quot;3100&quot;</span>, <span class="hljs-string">&quot;&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;JavaScript&quot;</span>]})
<span class="hljs-keyword">true</span>
</code></pre>
<h2 id="&#x83B7;&#x53D6;&#x6570;&#x636E;">&#x83B7;&#x53D6;&#x6570;&#x636E;</h2>
<p>ETS &#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E9B;&#x65B9;&#x4FBF;&#x597D;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x6211;&#x4EEC;&#x50A8;&#x5B58;&#x4E8E;&#x5176;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x5982;&#x4F55;&#x901A;&#x8FC7;&#x67E5;&#x8BE2;&#x952E;&#x548C;&#x51E0;&#x79CD;&#x4E0D;&#x540C;&#x5F62;&#x5F0F;&#x7684;&#x5F62;&#x5F0F;&#x5339;&#x914D;&#x6765;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<p>&#x6700;&#x5E38;&#x7528;&#xFF0C;&#x6548;&#x7387;&#x4E5F;&#x6700;&#x9AD8;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x76F4;&#x63A5;&#x6839;&#x636E;&#x952E;&#x6765;&#x67E5;&#x8BE2;&#x3002;&#x5339;&#x914D;&#x7684;&#x65B9;&#x6CD5;&#x867D;&#x7136;&#x4E5F;&#x6709;&#x7528;&#xFF0C;&#x4F46;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x8981;&#x904D;&#x5386;&#x6574;&#x5F20;&#x8868;&#xFF0C;&#x5728;&#x8F83;&#x5927;&#x7684;&#x6570;&#x636E;&#x96C6;&#x4E0A;&#x4F7F;&#x7528;&#x65F6;&#x8981;&#x7279;&#x522B;&#x8C28;&#x614E;&#x3002;</p>
<h3 id="&#x67E5;&#x8BE2;&#x952E;">&#x67E5;&#x8BE2;&#x952E;</h3>
<p>&#x4F7F;&#x7528; <code>lookup/2</code>&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4E00;&#x4E2A;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x6240;&#x6709;&#x8BB0;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.lookup(<span class="hljs-symbol">:user_lookup</span>, <span class="hljs-string">&quot;doomspork&quot;</span>)
[{<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]}]
</code></pre>
<h3 id="&#x7B80;&#x5355;&#x7684;&#x5339;&#x914D;">&#x7B80;&#x5355;&#x7684;&#x5339;&#x914D;</h3>
<p>ETS &#x662F;&#x4E3A; Erlang &#x6253;&#x9020;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x5339;&#x914D;&#x7684;&#x8BED;&#x6CD5;&#x53EF;&#x80FD;&#x6709;&quot;&#x4E00;&#x70B9;&#x70B9;&quot;&#x7B28;&#x91CD;.</p>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x539F;&#x5B50; <code>:&quot;$1&quot;</code>&#x3001;<code>:&quot;$2&quot;</code>&#x3001;<code>:&quot;$3&quot;</code> &#x7B49;&#x7B49;&#x6765;&#x8868;&#x793A;&#x5339;&#x914D;&#x4E2D;&#x6240;&#x4F7F;&#x7528;&#x7684;&#x53D8;&#x91CF;&#x3002;&#x5176;&#x4E2D;&#x7684;&#x6570;&#x5B57;&#x53EA;&#x7528;&#x6765;&#x8868;&#x793A;&#x5176;&#x5728;&#x8FD4;&#x56DE;&#x503C;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x800C;&#x975E;&#x5339;&#x914D;&#x65F6;&#x7684;&#x4F4D;&#x7F6E;&#x3002;&#x4E0D;&#x60F3;&#x8981;&#x7684;&#x90E8;&#x5206;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528; <code>:&quot;_&quot;</code> &#x6765;&#x5FFD;&#x7565;&#x6389;&#x3002;</p>
<p>&#x5339;&#x914D;&#x8868;&#x8FBE;&#x5F0F;&#x91CC;&#x4E5F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5199;&#x4E66;&#x9762;&#x503C;&#xFF0C;&#x4F46;&#x53EA;&#x6709;&#x53D8;&#x91CF;&#x8868;&#x793A;&#x7684;&#x90E8;&#x5206;&#x4F1A;&#x4F5C;&#x4E3A;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#x3002;&#x8BF4;&#x8D77;&#x6765;&#x592A;&#x62BD;&#x8C61;&#x4E86;&#x4E0D;&#x5982;&#x5B9E;&#x9645;&#x8BD5;&#x8BD5;&#x770B;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.match(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>, <span class="hljs-string">&quot;Sean&quot;</span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;_&quot;</span></span>})
[[<span class="hljs-string">&quot;doomspork&quot;</span>]]
</code></pre>
<p>&#x6211;&#x4EEC;&#x518D;&#x770B;&#x770B;&#x53D8;&#x91CF;&#x5982;&#x4F55;&#x5F71;&#x54CD;&#x7ED3;&#x679C;&#x7684;&#x987A;&#x5E8F;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.match(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-symbol">:<span class="hljs-string">&quot;$99&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;$3&quot;</span></span>})
[[<span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>], <span class="hljs-string">&quot;doomspork&quot;</span>],
 [<span class="hljs-string">&quot;&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;JavaScript&quot;</span>], <span class="hljs-string">&quot;3100&quot;</span>]]
</code></pre>
<p>&#x5047;&#x5982;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x83B7;&#x53D6;&#x7684;&#x662F;&#x672C;&#x6765;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5217;&#x8868;&#x5462;&#xFF1F;&#x90A3;&#x53EF;&#x4EE5;&#x7528; <code>match_object/2</code>&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5FFD;&#x7565;&#x90A3;&#x4E9B;&#x53D8;&#x91CF;&#x800C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x6574;&#x4E2A;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.match_object(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;_&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;$3&quot;</span></span>})
[{<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]},
 {<span class="hljs-string">&quot;3100&quot;</span>, <span class="hljs-string">&quot;&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;JavaScript&quot;</span>]}]

iex&gt; <span class="hljs-symbol">:ets</span>.match_object(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-symbol">:<span class="hljs-string">&quot;_&quot;</span></span>, <span class="hljs-string">&quot;Sean&quot;</span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;_&quot;</span></span>})
[{<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]}]
</code></pre>
<h3 id="&#x9AD8;&#x7EA7;&#x7684;&#x67E5;&#x8BE2;">&#x9AD8;&#x7EA7;&#x7684;&#x67E5;&#x8BE2;</h3>
<p>&#x770B;&#x8FC7;&#x4E86;&#x7B80;&#x5355;&#x5339;&#x914D;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6709;&#x6CA1;&#x6709;&#x66F4;&#x9AD8;&#x7EA7;&#x7684;&#x67E5;&#x8BE2;&#x65B9;&#x6CD5;&#x5462;&#xFF1F;&#x6BD4;&#x5982;&#x50CF; SQL &#x67E5;&#x8BE2;&#x90A3;&#x6837;&#x7684;&#xFF1F;&#x786E;&#x5B9E;&#x8FD8;&#x6709;&#x4E00;&#x5957;&#x66F4;&#x5F3A;&#x5927;&#x7684;&#x8BED;&#x6CD5;&#x53EF;&#x4EE5;&#x7528;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x4E09;&#x5143;&#x7EC4;&#x7136;&#x540E;&#x4F7F;&#x7528; <code>select/2</code> &#x6765;&#x505A;&#x66F4;&#x9AD8;&#x7EA7;&#x7684;&#x67E5;&#x8BE2;&#x3002;&#x8FD9;&#x4E2A;&#x4E09;&#x5143;&#x7EC4;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x5206;&#x522B;&#x8868;&#x793A;&#x6211;&#x4EEC;&#x7684;&#x5339;&#x914D;&#x6A21;&#x5F0F;&#xFF0C;&#x4E00;&#x4E9B;&#x300C;&#x536B;&#x5175;&#x300D;&#x8BED;&#x53E5; (guard)&#xFF0C;&#x4EE5;&#x53CA;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x7684;&#x683C;&#x5F0F;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7B80;&#x5355;&#x5339;&#x914D;&#x4E2D;&#x8BB2;&#x5230;&#x7684;&#x53D8;&#x91CF;&#x5F62;&#x5F0F;&#x5728;&#x52A0;&#x4E0A; <code>:&quot;$$&quot;</code> &#x4EE5;&#x53CA; <code>:&quot;$_&quot;</code> &#x6765;&#x6784;&#x5EFA;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x683C;&#x5F0F;&#x3002;&#x524D;&#x8005;&#x5C06;&#x7ED3;&#x679C;&#x53D8;&#x6210;&#x5217;&#x8868;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#xFF0C;&#x540E;&#x8005;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x539F;&#x59CB;&#x6570;&#x636E;&#x7684;&#x683C;&#x5F0F;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x628A;&#x524D;&#x9762;&#x7528; <code>match/2</code> &#x7684;&#x4F8B;&#x5B50;&#x6362;&#x6210; <code>select/2</code> &#x770B;&#x770B;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.match_object(<span class="hljs-symbol">:user_lookup</span>, {<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;_&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;$3&quot;</span></span>})
[{<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]},
 {<span class="hljs-string">&quot;3100&quot;</span>, <span class="hljs-string">&quot;&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;JavaScript&quot;</span>]}]

{% raw %}iex&gt; <span class="hljs-symbol">:ets</span>.select(<span class="hljs-symbol">:user_lookup</span>, [{{<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;_&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;$3&quot;</span></span>}, [], [<span class="hljs-symbol">:<span class="hljs-string">&quot;$_&quot;</span></span>]}]){% endraw %}
[{<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]},
 {<span class="hljs-string">&quot;spork&quot;</span>, <span class="hljs-number">30</span>, [<span class="hljs-string">&quot;ruby&quot;</span>, <span class="hljs-string">&quot;elixir&quot;</span>]}]
</code></pre>
<p>&#x867D;&#x7136; <code>select/2</code> &#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x66F4;&#x7EC6;&#x5FAE;&#x5730;&#x63A7;&#x5236;&#x5982;&#x4F55;&#x5339;&#x914D;&#xFF0C;&#x4EE5;&#x53CA;&#x8FD4;&#x56DE;&#x7684;&#x683C;&#x5F0F;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E2A;&#x8BED;&#x6CD5;&#x5B9E;&#x5728;&#x662F;&#x5F88;&#x4E0D;&#x53CB;&#x597D;&#xFF0C;&#x800C;&#x4E14;&#x8868;&#x8FBE;&#x80FD;&#x529B;&#x4E5F;&#x6709;&#x9650;&#x3002;&#x5176;&#x5B9E; ETS &#x8FD8;&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86; <code>fun2ms/1</code>&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5C06;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8F6C;&#x6362;&#x6210;&#x67E5;&#x8BE2;&#x65F6;&#x9700;&#x8981;&#x7528;&#x7684;&#xFF62;&#x5339;&#x914D;&#x89C4;&#x8303;&#xFF63; (<code>match_spec</code>)&#x3002;<code>fun2ms/1</code> &#x8BA9;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x66F4;&#x719F;&#x6089;&#x7684;&#x51FD;&#x6570;&#x5199;&#x6CD5;&#x6765;&#x6784;&#x5EFA;&#x5177;&#x4F53;&#x7684;&#x67E5;&#x8BE2;&#x903B;&#x8F91;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x8BD5;&#x8BD5;&#x7528; <code>fun2ms/1</code> &#x548C; <code>select/2</code> &#x6765;&#x627E;&#x51FA;&#x6240;&#x6709;&#x4F1A;&#x4E24;&#x79CD;&#x4EE5;&#x4E0A;&#x8BED;&#x8A00;&#x7684;&#x7528;&#x6237;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; fun = <span class="hljs-symbol">:ets</span>.fun2ms(<span class="hljs-keyword">fn</span> {username, <span class="hljs-number">_</span>, langs} <span class="hljs-keyword">when</span> length(langs) &gt; <span class="hljs-number">2</span> -&gt; username <span class="hljs-keyword">end</span>)
{% raw %}[{{<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;_&quot;</span></span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;$2&quot;</span></span>}, [{<span class="hljs-symbol">:&gt;</span>, {<span class="hljs-symbol">:length</span>, <span class="hljs-symbol">:<span class="hljs-string">&quot;$2&quot;</span></span>}, <span class="hljs-number">2</span>}], [<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>]}]{% endraw %}

iex&gt; <span class="hljs-symbol">:ets</span>.select(<span class="hljs-symbol">:user_lookup</span>, fun)
[<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;3100&quot;</span>]
</code></pre>
<p>&#x60F3;&#x66F4;&#x6DF1;&#x5165;&#x5730;&#x4E86;&#x89E3;&#x5339;&#x914D;&#x89C4;&#x8303;&#x8BF7;&#x53C2;&#x8003; Erlang &#x6709;&#x5173; <a href="http://www.erlang.org/doc/apps/erts/match_spec.html" target="_blank"><code>match_spec</code> &#x7684;&#x5B98;&#x65B9;&#x6587;&#x6863;</a>&#x3002;</p>
<h2 id="&#x5220;&#x9664;&#x6570;&#x636E;">&#x5220;&#x9664;&#x6570;&#x636E;</h2>
<h3 id="&#x5220;&#x9664;&#x8BB0;&#x5F55;">&#x5220;&#x9664;&#x8BB0;&#x5F55;</h3>
<p>&#x8DDF; <code>insert/2</code> &#x548C; <code>lookup/2</code> &#x5DEE;&#x4E0D;&#x591A;&#xFF0C;&#x6211;&#x4EEC;&#x7528; <code>delete/2</code> &#x6765;&#x5220;&#x9664;&#x67D0;&#x4E2A;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x8BB0;&#x5F55;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4F1A;&#x540C;&#x65F6;&#x5220;&#x9664;&#x952E;&#x548C;&#x503C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.delete(<span class="hljs-symbol">:user_lookup</span>, <span class="hljs-string">&quot;doomspork&quot;</span>)
<span class="hljs-keyword">true</span>
</code></pre>
<h3 id="&#x5220;&#x9664;&#x8868;">&#x5220;&#x9664;&#x8868;</h3>
<p>&#x53EA;&#x8981;&#x62E5;&#x6709;&#x8005;&#x6CA1;&#x6709;&#x9000;&#x51FA;&#xFF0C;ETS &#x8868;&#x5C31;&#x4E0D;&#x4F1A;&#x88AB;&#x5783;&#x573E;&#x56DE;&#x6536;&#x3002;&#x6709;&#x65F6;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x4FDD;&#x7559;&#x62E5;&#x6709;&#x8005;&#x8FDB;&#x7A0B;&#x7684;&#x540C;&#x65F6;&#x5220;&#x9664;&#x6574;&#x5F20;&#x8868;&#x3002;&#x8FD9;&#x4E2A;&#x64CD;&#x4F5C;&#x8981;&#x7528;&#x5230; <code>delete/1</code>&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:ets</span>.delete(<span class="hljs-symbol">:user_lookup</span>)
<span class="hljs-keyword">true</span>
</code></pre>
<h2 id="ets-&#x7684;&#x7528;&#x4F8B;">ETS &#x7684;&#x7528;&#x4F8B;</h2>
<p>&#x8BB2;&#x4E86;&#x8FD9;&#x4E48;&#x591A;&#xFF0C;&#x6211;&#x4EEC;&#x63A5;&#x4E0B;&#x6765;&#x628A;&#x5B66;&#x5230;&#x7684;&#x4E1C;&#x897F;&#x7EC4;&#x5408;&#x8D77;&#x6765;&#x505A;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x7F13;&#x5B58;&#x8BD5;&#x8BD5;&#x3002;&#x6211;&#x4EEC;&#x8981;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A; <code>get/4</code> &#x7684;&#x51FD;&#x6570;&#xFF0C;&#x63A5;&#x53D7;&#x6A21;&#x5757;&#x3001;&#x51FD;&#x6570;&#x3001;&#x53C2;&#x6570;&#x4EE5;&#x53CA;&#xFF08;&#x9488;&#x5BF9;&#x7F13;&#x5B58;&#x7684;&#xFF09;&#x9009;&#x9879;&#x3002;&#x76EE;&#x524D;&#x6211;&#x4EEC;&#x53EA;&#x5B9E;&#x73B0; <code>:ttl</code> &#x8FD9;&#x4E00;&#x4E2A;&#x9009;&#x9879;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x5047;&#x5B9A; ETS &#x8868;&#x5DF2;&#x7ECF;&#x7531;&#x5176;&#x4ED6;&#x7684;&#x8FDB;&#x7A0B;&#xFF08;&#x4F8B;&#x5982;&#x4E00;&#x4E2A;&#x76D1;&#x7763;&#x8005;&#xFF09;&#x542F;&#x52A8;&#x597D;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">SimpleCache</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-variable">@moduledoc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  A simple ETS based cache for expensive function calls.
  &quot;</span><span class="hljs-string">&quot;&quot;</span>

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  Retrieve a cached value or apply the given function caching and returning
  the result.
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span></span>(mod, fun, args, opts \\ []) <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">case</span> lookup(mod, fun, args) <span class="hljs-keyword">do</span>
      <span class="hljs-keyword">nil</span> -&gt;
        ttl = Keyword.get(opts, <span class="hljs-symbol">:ttl</span>, <span class="hljs-number">3600</span>)
        cache_apply(mod, fun, args, ttl)

      result -&gt;
        result
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  Lookup a cached result and check the freshness
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">lookup</span></span>(mod, fun, args) <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">case</span> <span class="hljs-symbol">:ets</span>.lookup(<span class="hljs-symbol">:simple_cache</span>, [mod, fun, args]) <span class="hljs-keyword">do</span>
      [result | <span class="hljs-number">_</span>] -&gt; check_freshness(result)
      [] -&gt; <span class="hljs-keyword">nil</span>
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  Compare the result expiration against the current system time.
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">check_freshness</span></span>({mfa, result, expiration}) <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">cond</span> <span class="hljs-keyword">do</span>
      expiration &gt; <span class="hljs-symbol">:os</span>.system_time(<span class="hljs-symbol">:seconds</span>) -&gt; result
      <span class="hljs-symbol">:else</span> -&gt; <span class="hljs-keyword">nil</span>
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>

  <span class="hljs-variable">@doc</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-string">&quot;
  Apply the function, calculate expiration, and cache the result.
  &quot;</span><span class="hljs-string">&quot;&quot;</span>
  <span class="hljs-function"><span class="hljs-keyword">defp</span> <span class="hljs-title">cache_apply</span></span>(mod, fun, args, ttl) <span class="hljs-keyword">do</span>
    result = apply(mod, fun, args)
    expiration = <span class="hljs-symbol">:os</span>.system_time(<span class="hljs-symbol">:seconds</span>) + ttl
    <span class="hljs-symbol">:ets</span>.insert(<span class="hljs-symbol">:simple_cache</span>, {[mod, fun, args], result, expiration})
    result
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x7528;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x7CFB;&#x7EDF;&#x65F6;&#x95F4;&#x7684;&#x51FD;&#x6570;&#x6765;&#x6F14;&#x793A;&#x8FD9;&#x4E2A;&#x7F13;&#x5B58;&#xFF0C;TTL &#x8BBE;&#x5B9A;&#x4E3A;10&#x79D2;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6211;&#x4EEC;&#x5728;&#x7F13;&#x5B58;&#x8FC7;&#x671F;&#x4E4B;&#x524D;&#x62FF;&#x5230;&#x7684;&#x90FD;&#x662F; ETS &#x4E2D;&#x4FDD;&#x5B58;&#x7684;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleApp</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span></span> <span class="hljs-keyword">do</span>
    <span class="hljs-symbol">:os</span>.system_time(<span class="hljs-symbol">:seconds</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

iex&gt; <span class="hljs-symbol">:ets</span>.new(<span class="hljs-symbol">:simple_cache</span>, [<span class="hljs-symbol">:named_table</span>])
<span class="hljs-symbol">:simple_cache</span>
iex&gt; ExampleApp.test
<span class="hljs-number">1451089115</span>
iex&gt; SimpleCache.get(ExampleApp, <span class="hljs-symbol">:test</span>, [], <span class="hljs-symbol">ttl:</span> <span class="hljs-number">10</span>)
<span class="hljs-number">1451089119</span>
iex&gt; ExampleApp.test
<span class="hljs-number">1451089123</span>
iex&gt; ExampleApp.test
<span class="hljs-number">1451089127</span>
iex&gt; SimpleCache.get(ExampleApp, <span class="hljs-symbol">:test</span>, [], <span class="hljs-symbol">ttl:</span> <span class="hljs-number">10</span>)
<span class="hljs-number">1451089119</span>
</code></pre>
<p>&#x8FC7;&#x4E86;10&#x79D2;&#x540E;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x62FF;&#x5230;&#x65B0;&#x7684;&#x7ED3;&#x679C;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; ExampleApp.test
<span class="hljs-number">1451089131</span>
iex&gt; SimpleCache.get(ExampleApp, <span class="hljs-symbol">:test</span>, [], <span class="hljs-symbol">ttl:</span> <span class="hljs-number">10</span>)
<span class="hljs-number">1451089134</span>
</code></pre>
<p>&#x7EFC;&#x4E0A;&#x6240;&#x8FF0;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4E0D;&#x5F15;&#x5165;&#x4EFB;&#x4F55;&#x4F9D;&#x8D56;&#x5C31;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x53EF;&#x6269;&#x5C55;&#x7684;&#x9AD8;&#x901F;&#x7F13;&#x5B58;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x53EA;&#x662F; ETS &#x7684;&#x8BF8;&#x591A;&#x5E94;&#x7528;&#x573A;&#x666F;&#x4E4B;&#x4E00;&#x3002;</p>
<h2 id="&#x57FA;&#x4E8E;&#x78C1;&#x76D8;&#x7684;-ets-dets">&#x57FA;&#x4E8E;&#x78C1;&#x76D8;&#x7684; ETS (DETS)</h2>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x4E86;&#x89E3;&#x4E86; ETS &#x8FD9;&#x4E2A;&#x5185;&#x5B58;&#x5B58;&#x50A8;&#xFF0C;&#x90A3;&#x6709;&#x6CA1;&#x6709;&#x57FA;&#x4E8E;&#x78C1;&#x76D8;&#x7684;&#x5B58;&#x50A8;&#x5462;&#xFF1F;&#x6CA1;&#x9519;&#xFF0C;&#x6211;&#x4EEC;&#x6709;&#x300C;&#x57FA;&#x4E8E;&#x78C1;&#x76D8;&#x7684;&#x9879;&#x5F0F;&#x5B58;&#x50A8;&#x300D; (Disk Based Term Storage)&#xFF0C;&#x7B80;&#x79F0; DETS&#x3002;ETS &#x548C; DETS &#x7684; API &#x57FA;&#x672C;&#x4E0A;&#x662F;&#x901A;&#x7528;&#x7684;&#xFF0C;&#x53EA;&#x6709;&#x521B;&#x5EFA;&#x8868;&#x7684;&#x65B9;&#x5F0F;&#x6709;&#x4E9B;&#x8BB8;&#x4E0D;&#x540C;&#x3002;DETS &#x4F7F;&#x7528; <code>open_file/2</code> &#x800C;&#x4E14;&#x4E0D;&#x9700;&#x8981; <code>:named_table</code> &#x9009;&#x9879;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; {<span class="hljs-symbol">:ok</span>, table} = <span class="hljs-symbol">:dets</span>.open_file(<span class="hljs-symbol">:disk_storage</span>, [<span class="hljs-symbol">type:</span> <span class="hljs-symbol">:set</span>])
{<span class="hljs-symbol">:ok</span>, <span class="hljs-symbol">:disk_storage</span>}
iex&gt; <span class="hljs-symbol">:dets</span>.insert_new(table, {<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]})
<span class="hljs-keyword">true</span>
iex&gt; select_all = <span class="hljs-symbol">:ets</span>.fun2ms(&amp;(&amp;<span class="hljs-number">1</span>))
[{<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>, [], [<span class="hljs-symbol">:<span class="hljs-string">&quot;$1&quot;</span></span>]}]
iex&gt; <span class="hljs-symbol">:dets</span>.select(table, select_all)
[{<span class="hljs-string">&quot;doomspork&quot;</span>, <span class="hljs-string">&quot;Sean&quot;</span>, [<span class="hljs-string">&quot;Elixir&quot;</span>, <span class="hljs-string">&quot;Ruby&quot;</span>, <span class="hljs-string">&quot;Java&quot;</span>]}]
</code></pre>
<p>&#x73B0;&#x5728;&#x9000;&#x51FA; <code>iex</code> &#x4F60;&#x5C31;&#x80FD;&#x770B;&#x5230;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#x751F;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6587;&#x4EF6; <code>disk_storage</code>&#xFF1A;</p>
<pre><code class="lang-shell">$ ls | grep -c disk_storage
1
</code></pre>
<p>&#x6700;&#x540E;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x4E00;&#x70B9;&#xFF0C;DETS &#x4E0D;&#x652F;&#x6301; <code>ordered_set</code>&#xFF0C;&#x53EA;&#x652F;&#x6301; <code>set</code>&#x3001;<code>bag</code> &#x548C; <code>duplicate_bag</code>&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="eex.html" class="navigation navigation-prev " aria-label="Previous page: 嵌入的 Elixir(EEx)">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="mnesia.html" class="navigation navigation-next " aria-label="Next page: Mnesia 数据库">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.1.0","title":"Erlang 项式存储(ETS)","base_commit":"035ee14","level":"4.3","depth":1,"next":{"title":"Mnesia 数据库","level":"4.4","depth":1,"path":"lessons/specifics/mnesia.md","ref":"lessons/specifics/mnesia.md","articles":[]},"previous":{"title":"嵌入的 Elixir(EEx)","level":"4.2","depth":1,"path":"lessons/specifics/eex.md","ref":"lessons/specifics/eex.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/specifics/ets.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

